﻿using Models;
using Newtonsoft.Json;
using Ozone.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Models
{

    /// <summary>
    /// 
    /// </summary>
    public class DispensingModel
    {
        public List<DispensingMethod> ListMethod { set; get; }

        public DispensingModel()
        {
            ListMethod = new List<DispensingMethod>();
        }

    }

    public class DispensingMethod
    {

        public string FID { set; get; }
        public string FName { set; get; }

        public List<MethodItems> ListMethodItem { set; get; }

        public DispensingMethod()
        {
            ListMethodItem = new List<MethodItems>();
        }

        public void SortMethodItem()
        {
            for (int i = 0; i < ListMethodItem.Count; i++)
            {
                ListMethodItem[i].FNO_ = i + 1;
            }
        }

        public void Paste(MethodItems item)
        {
            try
            {
                string str = JsonConvert.SerializeObject(item);
                MethodItems newitem = JsonConvert.DeserializeObject<MethodItems>(str);
                ListMethodItem.Add(newitem);
            }
            catch (Exception ex)
            {

            }
        }

        public void ResetStatus()
        {
            for (int i = 0; i < ListMethodItem.Count; i++)
            {
                ListMethodItem[i].FStatus = 0;
            }
        }
    }

    //方法条目
    public class MethodItems
    {
        public void Show()
        {
            object obj = this;
            Type t = obj.GetType();//获取对象类型
            string strS = "";
            foreach (FieldInfo fi in t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                strS += ("名称:" + fi.Name + " 值：" + fi.GetValue(this) + ",类型:" + fi.FieldType.Name + "\n");

            Console.WriteLine(strS);
            foreach (FieldInfo fi in GetType().GetFields())
                Console.WriteLine($"{fi.FieldType} {fi.Name}: {fi.GetValue(this)}");
        }

        public List<ParamVal> getPVS()
        {
            List<ParamVal> lstPVS = new List<ParamVal>();
            object obj = this;
            Type t = obj.GetType();//获取对象类型
            foreach (PropertyInfo fi in t.GetProperties())
            {
                if (fi.Name.Contains("_"))
                    continue;

                string v = "";
                object val = fi.GetValue(obj);
                if (val != null)
                    v = val.ToString();
                lstPVS.Add(new ParamVal()
                {
                    FDataID = fi.Name,
                    FValue = v
                }
                );
                //                strS += ("名称:" + fi.Name + " 值：" + fi.GetValue(this) + ",类型:" + fi.FieldType.Name + "\n");
            }

            return lstPVS;

        }

        public int FNO_ { set; get; }

        //配液瓶序号(1-6) 空瓶
        public int FSolventBottleNo { get; set; }
        /*运行状态 0：未运行 
         *         1：等待中
         *         2：执行中
         *         3：保持中
         *         4：已完成 
         *         
         */
        [JsonIgnore]
        public int FStatus { set; get; }


        public string FStatusStr_
        {
            get
            {
                if (FStatus == 0) return "未执行";
                if (FStatus == 1) return "运行中";
                if (FStatus == 2) return "已完成";

                return "";
            }
        }


        //原料瓶配置 12个瓶子
        public List<ObtainParam> ListMaterialBottles { set; get; } = new List<ObtainParam>();
        //溶液瓶配置 6个瓶子
        public List<ObtainParam> ListSolutionBottles { set; get; } = new List<ObtainParam>();

        public string FMaterialBottlesStr
        {
            get
            {
                string str = "";
                for (int i = 0; i < ListMaterialBottles.Count; i++)
                {
                    str += (i + 1).ToString() + "、"
                        + "原料瓶号：" + ListMaterialBottles[i].FBottleNo + " "
                        + "原料名称：" + ListMaterialBottles[i].FName + " "
                        + "体积（ul）：" + ListMaterialBottles[i].FWeightVolume + " " + "ul"
                        + "\r\n"
                        ;
                }


                return str;
            }
        }

        public string FSolutionBottlesStr
        {
            get
            {
                string str = "";
                for (int i = 0; i < ListSolutionBottles.Count; i++)
                {
                    str += (i + 1).ToString() + "、"
                         + "溶液瓶号：" + ListSolutionBottles[i].FBottleNo + " "
                         + "溶液名称：" + ListSolutionBottles[i].FName + " "
                         + "重量（g）：" + ListSolutionBottles[i].FWeightVolume + " " + "g"
                         + "\r\n"
                         ;
                }


                return str;
            }
        }

        public MethodItems()
        {
            FStatus = 0;

        }
    }




    //获取液体参数，原料或者溶剂
    public class ObtainParam
    {
        //原料瓶序号(1-12)
        public int FBottleNo { get; set; } = -1;
        //名称
        public string FName { get; set; }
        //原料瓶的条码
        public string FBarcode { get; set; }
        //密度
        public string FDensity { get; set; }
        //原液重量或体积
        public string FWeightVolume { get; set; }

    }
}
